<?php
/*
	PookiWiki - A wiki unlike the rest.
	Copyright (C) 2012 Jordan Craw

	This program is free software; you can redistribute it and/or
	modify it under the terms of the GNU General Public License
	as published by the Free Software Foundation; either version 2
	of the License, or (at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the Free Software
	Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

class PookiCoreRequestHandler
{
	// Internal variables
	protected $request_type;
	protected $get_request;
	protected $post_request;
	protected $request;
	
	protected $supported_request_elements = array(
		"q" => array(),
		"args" => array()
	);
	protected $proper_request = array();

	public $path;
	public $arguments;
	
	protected function getRequestType()
	{		
		if(count($this->get_request) && !count($this->post_request))
		{
			$this->request_type = POOKIWIKI_CORE_REQUEST_TYPE_GET_REQUEST;
		}
		else if(count($this->post_request) && !count($this->get_request))
		{
			$this->request_type = POOKIWIKI_CORE_REQUEST_TYPE_POST_REQUEST;
		}
		else
		{
			$this->request_type = POOKIWIKI_CORE_REQUEST_TYPE_NO_REQUEST;
		}
	}
	
	protected function sanitizeRequest()
	{
		$request = $this->request_type == POOKIWIKI_CORE_REQUEST_TYPE_GET_REQUEST ? $this->get_request : $this->post_request;
		$reform_array = array_intersect_key($request,$this->supported_request_elements);
		
		if(empty($reform_array))
		{
			$this->request_type = POOKIWIKI_CORE_REQUEST_TYPE_DEFAULT_REQUEST;
		}
		else
		{
			$replace_func = function($value)
			{
				return preg_replace("#[^A-Za-z/_(),]#","",strtolower($value));
			};	
			$reform_array = array_map($replace_func,$reform_array);
			$values = array_map('strlen',array_keys(array_flip($reform_array)));
			$empty = !(bool) array_product($values);
			
			if($empty)
			{
				$this->request_type = POOKIWIKI_CORE_REQUEST_TYPE_DEFAULT_REQUEST;
				$reform_array = POOKIWIKI_CORE_REQUEST_TYPE_DEFAULT_REQUEST;
			}
		}
		
		$this->request = $reform_array;
	}
	
	
	// TODO: Make this dynamic so that classes which extend the supported_request_elements can have their elements validated, too
	protected function processRequest()
	{
		
		if($this->request != POOKIWIKI_CORE_REQUEST_TYPE_DEFAULT_REQUEST)
		{
			if(array_key_exists("q",$this->request))
			{

				// Check to see if it's either a normal string or a path-related string
				if(preg_match("#([A-Za-z_()-]+/?){1,}#",$this->request["q"],$matches))
				{
					if(isset($matches[0]))
					{
						$path = $matches[0];
						if($pos = strpos($path,"/"))
						{
							$last_pos = strrpos($path,"/");
							
							if($pos == 0)
							{
								$path = substr($path,1);
							}
							if($last_pos == (strlen($path)-1))
							{
								$path = substr($path,0,-1);
							}
							$this->path = explode("/",$path);
						}
						else
						{
							$this->path = $path;
						}
					}
				}
			}
			if(array_key_exists("args",$this->request))
			{
				if(preg_match("#(,)?([A-Za-z0-9]+)((,([A-Za-z0-9]+))+)?(,)?#",$this->request["args"],$matches))
				{
					if(isset($matches[0]))
					{
						$args = $matches[0];
	
						if(strpos($args,",",0) != FALSE && strpos($args,",",0) == 0)
						{
							$args = substr($args,1);
						}

						if(strpos($args,",",1) == strlen($args)-1)
						{
							$args = substr($args,0,-1);
						}
						
						$args_count = substr_count($args,",");
						
						if($args_count >=1)
						{
							$this->arguments = explode(",",$args);
						}
						else
						{
							$this->arguments = array($args);
						}
					}
				}
			}
		}
	}
	
	protected function initRequest()
	{
		$this->getRequestType();
		
		if($this->request_type == POOKIWIKI_CORE_REQUEST_TYPE_GET_REQUEST or $this->request_type == POOKIWIKI_CORE_REQUEST_TYPE_POST_REQUEST)
		{
			$this->sanitizeRequest();
			$this->processRequest();
		}
		else
		{
			$this->path = $this->request_type;
			$this->arguments = $this->request_type;
		}
	}
	
	public function __construct()
	{
		$this->get_request = $_GET;
		$this->post_request = $_POST;
		$this->initRequest();
	}
	
	
}

class PookiModuleHandler
{
	public $mode;
	
	protected $required_modules = array(
		'engine',
		'login',
		'page'
	);
	
	protected $available_modules;
	
	protected $present_core_modules;
	protected $present_additional_modules;
	
	protected function gatherAvailableModules()
	{
		$folder = new PookiFile("modules");
	
		if($folder->readable)
		{
			$module_files = new PookiFileReader($folder);
			$modules = $module_files->contents_array;
			$modules_available = count($modules);
			
			
			if($modules_available)
			{
				$this->present_core_modules = array_intersect($this->required_modules,$modules);
				$this->present_additional_modules = array_diff($modules,$this->required_modules);
				
				if(count($this->present_core_modules) == count($this->required_modules))
				{
					$this->mode = POOKI_CORE_OPERATING_MODE_FULL;
				}
				else if(count($this->present_core_modules) < count($this->required_modules))
				{
					$this->mode = POOKI_CORE_OPERATING_MODE_PARTIAL;	
				}
				else
				{
					$this->mode = POOKI_CORE_OPERATING_MODE_FAILSAFE;
				}	
			}
			else
			{
				$this->mode = POOKI_CORE_OPERATING_MODE_FAILSAFE;
			}
		}
	}
	
	
	protected function validateAvailableModules()
	{
		// Need to work on this after all the support includes have been finished.
	}
	
	public function __construct()
	{
		// Gather the available modules and add them to the stack. 
		$this->gatherAvailableModules();
		
		// Validate the available 
		$this->validateAvailableModules();
		
		
		
	}
	
}


class PookiFrontController
{
	protected $request_handler;

	protected function determineModuleHandler()
	{
		$path = $this->request_handler->path;
		$arguments = $this->request_handler->arguments;
	}
	
	
	
	public function __construct()
	{
		
		$this->request_handler = new PookiCoreRequestHandler();
		$this->determineModuleHandler();
		$module_handler = new PookiModuleHandler();
		
	}
	
	
	
}

class PookiModule
{
	
}

class PookiCore
{
	protected $theme_instance;
	protected $theme_object;
	protected $settings;
	protected $SQL;
	protected $controller;
	
	public function __construct()
	{
		$this->settings 		= new PookiSettings();
		$this->SQL 				= new PookiSQLLoader($this->settings);
		
		// This is where the PookiCore class will need to develop the HTML content for the PookiThemeInstance
		$this->controller		= new PookiFrontController();
		
		
		$this->theme_object 	= new PookiThemeInstance();
		$this->theme_instance 	= new PookiTheme($this->theme_object);	
		
		
	}
	
}



?>